home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************************
-
- NTBDemo.c - demo/test program for NeoTextBox
-
- Written by Bryan K. Ressler (Beaker), 8/30/91
-
- *****************************************************************************************/
-
- #define BEAKER
-
- /** INCLUDES ****************************************************************************/
- #include "NTBDemo.h" /* NTBDemo stuff */
- #include "Utilities.h" /* Utility stuff */
- #include "NeoTextBox.h" /* NeoTextBox module */
-
- /** GLOBALS *****************************************************************************/
- SysEnvRec gEnvironment; /* Our operating environment */
- Boolean gHasTrueType; /* Is TrueType around? */
- short gJustRadio; /* Currently-set justification radio button */
- short gFontRadio; /* Currently-set font radio button */
-
- /** STATICS *****************************************************************************/
- char gsBlankStr[1]; /* Blank string */
- short gsTextRadio; /* Currently-set text radio button */
- Boolean gsVariable; /* State of "Variable line height" checkbox */
- short gsNumLines; /* The number of line in the wrapped text total */
- short gsEndY; /* The endingY from NeoTextBox item */
- short gsLHUsed; /* The line height used based on the given text spec */
- Str255 gsInfoStr; /* The string displayed in the info item */
-
- /** PUBLIC PROTOTYPES *******************************************************************/
- void main(void);
-
- /** STATIC PROTOTYPES *******************************************************************/
- static pascal void NTBItem(WindowPtr theWindow,short itemNum);
- static pascal void NTBInfoItem(WindowPtr theWindow,short itemNum);
- static void NTBPerformance(DialogPtr dialog);
- static void UpdateParms(DialogPtr testDialog);
- static void RedrawInfo(DialogPtr dialog);
- static void SetupDialog(DialogPtr testDialog);
- static void PoseDialog(DialogPtr testDialog);
- static void DemoNTB(void);
- static Boolean Initialize(void);
-
- /****************************************************************************************/
- pascal void NTBItem(WindowPtr theWindow,short itemNum)
- {
- short aType;
- Rect theBox;
- Handle aHandle,text;
- long textLen;
- short just,lineHeight;
- Boolean oldPreserve,oldPreferred;
- short oldLH,oldNL,oldEY;
-
- GetDItem(theWindow,itemNum,&aType,&aHandle,&theBox);
- PenNormal(); PenPat(&qd.gray);
- ForeColor(blackColor);
- FrameRect(&theBox);
- PenNormal();
-
- InsetRect(&theBox,4,4);
- EraseRect(&theBox);
-
- text = GetResource('TEXT',(gsTextRadio == kShannonText) ? kShannonID : kBarkerID);
- if (text) {
- oldLH = gsLHUsed; /* Remember these so we can check for diffs */
- oldNL = gsNumLines;
- oldEY = gsEndY;
-
- just = RadioToJust(); /* Find out what justification to use */
- textLen = GetHandleSize(text); /* Determine text length */
- HLock(text); /* Lock text - NeoTextBox may move memory */
-
- TextParms(kSave); /* Save current port text parameters */
-
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
-
- if (gHasTrueType) {
- oldPreferred = GetOutlinePreferred(); /* Save old, prefer TrueType */
- SetOutlinePreferred(true);
-
- if (gsVariable) {
- oldPreserve = GetPreserveGlyph(); /* Save old, preserve glyphs */
- SetPreserveGlyph(true);
- }
- }
-
- lineHeight = (gsVariable) ? -1 : 0;
-
- /* This is the fancy-schmancy way, see InfoItem for a simple example */
- gsNumLines = NeoTextBox(*text,textLen,&theBox,just,lineHeight,&gsEndY,&gsLHUsed);
-
- if (gHasTrueType) { /* Restore TrueType settings */
- SetOutlinePreferred(oldPreferred);
- if (gsVariable)
- SetPreserveGlyph(oldPreserve);
- }
-
- TextParms(kRestore); /* Restore the port's old text parameters */
-
- if (gsEndY <= theBox.bottom) {
- PenPat(&qd.ltGray); /* Draw a light gray line below the text */
- PenMode(patOr);
- ForeColor(blueColor);
- MoveTo(theBox.left,gsEndY);
- LineTo(theBox.right - 1,gsEndY);
- ForeColor(blackColor);
- PenNormal();
- }
-
- if (gsLHUsed != oldLH || gsNumLines != oldNL || gsEndY != oldEY)
- UpdateParms(theWindow); /* Redraw the stats if they've changed */
- }
- }
-
- /****************************************************************************************/
- pascal void NTBInfoItem(WindowPtr theWindow,short itemNum)
- {
- short aType;
- Rect theBox;
- Handle aHandle;
-
- GetDItem(theWindow,itemNum,&aType,&aHandle,&theBox);
- ForeColor(blackColor);
-
- EraseRect(&theBox);
-
- TextParms(kSave); /* Save current port text parameters */
-
- TextFont(GetAppFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(9);
- TextMode(srcOr);
-
- /* This is the simple, TextBox-like way to call NeoTextBox */
- NeoTextBox(gsInfoStr + 1,*gsInfoStr,&theBox,GetSysJust(),0,nil,nil);
-
- TextParms(kRestore); /* Restore the port's old text parameters */
- }
-
- /****************************************************************************************/
- void NTBPerformance(DialogPtr dialog)
- {
- char buf1[40],buf2[40],buf3[40],buf4[40];
- Handle testTextHdl;
- char *testText;
- long testLen;
- Rect box;
- short i,just,percentFaster;
- long startTime,endTime,textBoxTime,neoTextBoxTime;
- Fixed factor;
- Boolean goForIt = true;
-
- just = RadioToJust(); /* Set up justification */
- testTextHdl = GetResource('TEXT',kTestTextID);
-
- if (just == ntbJustFull) {
- goForIt = false;
- StopAlert(kNoCanDoAlrtID,nil);
- }
-
- if (goForIt) {
- NumToString(kTestIterations,(Str255)buf1); /* Put stats dialog up */
- ParamText(buf1,gsBlankStr,gsBlankStr,gsBlankStr);
- goForIt = (NoteAlert(kDoPerfAlrtID,nil) == kOkayButton);
- }
-
- if (goForIt && testTextHdl) {
- testLen = GetHandleSize(testTextHdl);
- HLock(testTextHdl);
- testText = *testTextHdl;
-
- GetItemRect(dialog,kTextArea,&box);
- InsetRect(&box,4,4);
-
- GetIndString(gsInfoStr,kStringsID,kCachingPass);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- TextBox(testText,testLen,&box,just);
- EraseRect(&box);
- NeoTextBox(testText,testLen,&box,just,0,nil,nil);
- TextParms(kRestore); /* Restore port's text settings */
-
- GetIndString(gsInfoStr,kStringsID,kTestingTextBox);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- startTime = TickCount();
- for (i = 0; i < kTestIterations; i++)
- TextBox(testText,testLen,&box,just);
- endTime = TickCount();
- textBoxTime = endTime - startTime;
- TextParms(kRestore); /* Restore port's text settings */
-
- GetIndString(gsInfoStr,kStringsID,kTestingNeoTextBox);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- startTime = TickCount();
- for (i = 0; i < kTestIterations; i++) {
- EraseRect(&box);
- NeoTextBox(testText,testLen,&box,just,0,nil,nil);
- }
- endTime = TickCount();
- neoTextBoxTime = endTime - startTime;
- TextParms(kRestore); /* Restore port's text settings */
-
- HUnlock(testTextHdl);
-
- NumToString(kTestIterations,(Str255)buf1); /* Put stats dialog up */
- NumToString(textBoxTime,(Str255)buf2);
- NumToString(neoTextBoxTime,(Str255)buf3);
- factor = FixRatio(textBoxTime,neoTextBoxTime) - Long2Fix(1);
- percentFaster = FixRound(FixMul(factor,Long2Fix(100)));
- NumToString(percentFaster,buf4);
- ParamText(buf1,buf2,buf3,buf4);
- NoteAlert(kPerfAlrtID,nil);
-
- GetIndString(gsInfoStr,kStringsID,kStdInfo); /* Restore info item text */
- InvalItem(dialog,kInfoItem,0,0);
- InvalItem(dialog,kTextArea,4,4);
- }
-
- UpdateParms(dialog);
- }
-
- /****************************************************************************************/
- void UpdateParms(DialogPtr dialog)
- {
- char convBuffer1[40],convBuffer2[40],convBuffer3[40];
-
- NumToString(gsEndY,(Str255)convBuffer1);
- NumToString(gsLHUsed,(Str255)convBuffer2);
- NumToString(gsNumLines,(Str255)convBuffer3);
- ParamText(convBuffer1,convBuffer2,convBuffer3,gsBlankStr);
-
- InvalItem(dialog,kValuesItem,0,0);
- }
-
- /****************************************************************************************/
- void RedrawInfo(DialogPtr dialog)
- {
- InvalItem(dialog,kInfoItem,0,0);
- BeginUpdate(dialog);
- UpdtDialog(dialog,dialog->visRgn);
- EndUpdate(dialog);
- }
-
- /****************************************************************************************/
- void SetupDialog(DialogPtr testDialog)
- {
- short i;
-
- gJustRadio = JustToRadio(GetSysJust()); /* Default to system justification */
- gsVariable = false; /* Default to default line height */
- gFontRadio = kAppFont; /* Default to application font */
- gsTextRadio = kShannonText; /* Default to Shannon text */
-
- GetIndString(gsInfoStr,kStringsID,kStdInfo); /* Set up info item */
-
- SetValue(testDialog,gJustRadio,1); /* Set initial values */
- SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
- SetValue(testDialog,gFontRadio,1);
- SetValue(testDialog,gsTextRadio,1);
-
- for (i = kBoxOne; i <= kLastBox; i++)
- UserItem(testDialog,i,BoxItem); /* Install box user items */
- for (i = kDividerOne; i <= kLastDivider; i++)
- UserItem(testDialog,i,GrayBoxItem); /* Install divider user items */
- UserItem(testDialog,kTextArea,NTBItem); /* Install the main text item */
- UserItem(testDialog,kInfoItem,NTBInfoItem); /* Install the information item */
-
- NTBItem(testDialog,kTextArea); /* Draw (to set Y/line height vars) */
-
- ShowWindow(testDialog); /* Put it on the screen */
- }
-
- /****************************************************************************************/
- void PoseDialog(DialogPtr testDialog)
- {
- short itemHit;
-
- do {
- ModalDialog(nil,&itemHit);
- switch (itemHit) {
- case kPerfButton:
- NTBPerformance(testDialog);
- break;
- case kJustLeft: /* Justification cluster */
- case kJustCenter:
- case kJustRight:
- case kJustFull:
- if (gJustRadio != itemHit) {
- SetValue(testDialog,gJustRadio,0);
- gJustRadio = itemHit;
- SetValue(testDialog,gJustRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- case kVariableHeight: /* Variable height checkbox */
- gsVariable = !gsVariable;
- SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
- InvalItem(testDialog,kTextArea,4,4);
- break;
- case kAppFont: /* Font cluster */
- case kTimes:
- case kHelvetica:
- if (gFontRadio != itemHit) {
- SetValue(testDialog,gFontRadio,0);
- gFontRadio = itemHit;
- SetValue(testDialog,gFontRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- case kShannonText: /* Test cluster */
- case kLiterature:
- if (gsTextRadio != itemHit) {
- SetValue(testDialog,gsTextRadio,0);
- gsTextRadio = itemHit;
- SetValue(testDialog,gsTextRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- default:
- break;
- }
- } while (itemHit != kQuitButton);
- }
-
- /****************************************************************************************/
- void DemoNTB(void)
- {
- GrafPtr savePort;
- DialogPtr testDialog;
-
- GetPort(&savePort);
- testDialog = GetNewDialog(kTestDlgID,nil,(WindowPtr)-1);
- if (testDialog) {
- SetPort(testDialog);
- SetupDialog(testDialog);
- PoseDialog(testDialog);
- DisposDialog(testDialog);
- }
- SetPort(savePort);
- }
-
- /****************************************************************************************/
- Boolean Initialize(void)
- {
- OSErr err;
-
- InitGraf(&qd.thePort); /* The usual Mac inits */
- InitFonts();
- InitWindows();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- *gsBlankStr = 0;
- gHasTrueType = (NGetTrapAddress(kTrueTypeTrap,ToolTrap) !=
- NGetTrapAddress(kUnimplTrap,ToolTrap));
-
- err = SysEnvirons(2,&gEnvironment);
- return((err || gEnvironment.systemVersion < 0x0600) ? false : true);
- }
-
- /****************************************************************************************/
- void main(void)
- {
- if (Initialize())
- DemoNTB();
- }
-